/**
 *
 */
package pt.mleiria.utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

/**
 * @author manuel
 *
 */
public class MathUtils {

    private final static Logger LOG = Logger.getLogger(MathUtils.class.getName());

    private static final Map<String, Integer> tabMap = new HashMap<String, Integer>(100);
    private static final List<String> operacoes = new ArrayList<String>(100);

    private static final int[] firstTerm = new int[10];
    private static final int[] secondTerm = new int[10];

    static {
        for (int i = 0; i < 10; i++) {
            firstTerm[i] = secondTerm[i] = i + 1;
        }
    }

    public static int[] tabuadaCompleta() {
        for (int i = 0; i < 10; i++) {
            LOG.info("Tabuada dos:" + (i + 1));
            for (int j = 0; j < 10; j++) {
                final String operacao = firstTerm[i] + " x " + secondTerm[j] + " = ";
                final int resultado = firstTerm[i] * secondTerm[j];
                tabMap.put(operacao, resultado);
                operacoes.add(operacao);
                LOG.info(operacao + resultado);
            }
        }
        Collections.shuffle(operacoes);
        return null;
    }

    public static void main(String[] args) {
        System.out.println(0XFF);
    }

    public static String fromCharToHexa(final char ch) {
        return String.format("%04x", (int) ch);
    }

    public static byte[] StringToBytesASCII(final String str) {
        final byte[] b = new byte[str.length()];
        for (int i = 0; i < b.length; i++) {
            b[i] = (byte) str.charAt(i);
        }
        return b;
    }

    public static String HexaStringToChar(final String hexaString) {
        StringBuilder output = new StringBuilder();
        for (int i = 0; i < hexaString.length(); i += 2) {
            String str = hexaString.substring(i, i + 2);
            output.append((char) Integer.parseInt(str, 16));
        }
        return output.toString();
    }

    public static byte[] hexStringToByteArray(String s) {
        int len = s.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                    + Character.digit(s.charAt(i + 1), 16));
        }
        return data;
    }
    final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
    /**
     * 
     * @param bytes
     * @return 
     */
    public static String bytesToHex(byte[] bytes) {
        char[] hexChars = new char[bytes.length * 2];
        for (int j = 0; j < bytes.length; j++) {
            int v = bytes[j] & 0xFF;
            hexChars[j * 2] = hexArray[v >>> 4];
            hexChars[j * 2 + 1] = hexArray[v & 0x0F];
        }
        return new String(hexChars);
    }

    public static int mod(int a, int b) {
        if (b < 0) //you can check for b == 0 separately and do what you want
        {
            return mod(-a, -b);
        }
        int ret = a % b;
        if (ret < 0) {
            ret += b;
        }
        return ret;
    }
}
